// pages/newUser/identification/identification.js
import util from "../../../utils/util.js";
import api from "../../../common/api.js";
var app = getApp();
var page = 1;
var clearDynamicState;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    src:'',
    num:0,
    height: 0,
    testlist: []
  },
  redPackage(){
    wx.showToast({
      title: '提交成功，审核通过后红包将发放至您的账户',
      icon:'none',
      duration: 1000,
      success: function(){
        app.globalData.newUser = true;                                                                                            
        // setTimeout(function(){
          wx.reLaunch({
            url: '/pages/index/index',
          })
        // },100)
      }
    })
  },
  goTOto(){
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds: [
        '0DtRGEfwrfLkgotBwyPFXmerz4NF19KdJxaV_M2wjTo',
        'P8NAay-gJ16j7zhWuNWgMC9YJFqQzF4Y0aKk3TUweFw',
        'qg59ynsFGAsbM65bsuu9NSBLfY1P47d1QlUCZsgkit8'
      ],
      success(res) {
        
      },
      fail(res) {
        console.log(res) /*20004-errorCode*/
       
      },
      complete(res) {
       
        that.redPackage()
      }
    })
  },
  chooseimage(){
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0];
        util.uploadFiles(src, (res)=>{
          try {
            that.setData({
              src,
              fileId:JSON.parse(res.data)[0].id
            })
          } catch (error) {
            
          }
        })
      }
    })
  },
  submit(){
    api.examinePmActivityOrderInfo({
      "pmActivityOrderInfo": {
        "id": this.data.id,
      },
      "pmActivityOrderAttachDetail": {
          "sourceOrderId":  this.data.id,
          "fileId": this.data.fileId
        }
    },res=>{
      // if(res.data.isSuccess){
        this.goTOto();
      // } else {
      //   wx.showToast({
      //     title: res.data.errorMessage,
      //     icon: 'none'
      //   })
      // }
    })
  },
  // 红包动态
  dyNamicState(){
    api.dyNamicState({
      "page":page,
      "pageSize":"10"
    },res=>{
      var list = this.data.testlist;
      if(res.data.content.rows.length>0){
        list = list.concat(res.data.content.rows)
      }
      this.setData({
        testlist: list
      })
    })
  },
  // 截获竖向滑动
  catchTouchMove:function(res){
    return false
  },
  clearDynamicState(){
    page += 1;
    this.dyNamicState();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    page = 1;
    var height = app.globalData.windowHeight - 400;
    var num = Math.floor(app.globalData.windowHeight*0.7/100);
    var id = options.id
    that.setData({
      height,
      num,
      id,
      current: 0
    })
    app.globalData.loginPromise
      .then((content) => {
        that.dyNamicState();
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    page = 1;
    clearDynamicState =  setInterval(this.clearDynamicState,10000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(clearDynamicState);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(clearDynamicState);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})